﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using ComForm.Properties;

namespace ComForm
{
    public partial class FormSelectPinYin : MetroForm
    {
        private int nSel = 0;
        public int SelectIndex
        {
            get { return nSel; }           
        }

        private List<Button> listBtns = new List<Button>();

        private List<RadioButton> listRadioBtns = new List<RadioButton>();

        private List<string> listSels = new List<string>();
        private string chineseText;
        public List<string> listUserSels = new List<string>();

        public FormSelectPinYin()
        {
            InitializeComponent();
            listBtns.Add(button1);
            listBtns.Add(button2);
            listBtns.Add(button3);           
            listRadioBtns.Add(radioButton1);
            listRadioBtns.Add(radioButton2);
            listRadioBtns.Add(radioButton3);          
        }

        public FormSelectPinYin(string ch, List<string> list) : this()
        {
            chineseText = ch;
            listSels = list;
        }

        private void InitButtonOK()
        {
            int x = (this.Width - buttonOK.Width) / 2;
            buttonOK.Location = new Point(x, buttonOK.Top);
        }

        private void InitUserSel(int index)
        {
            if (listRadioBtns[index].Checked)
            {
                return;
            }
            listRadioBtns[index].Checked = true;
            for (int i = 0; i < listBtns.Count; i++)
            {
                if (index == i)
                {
                    listBtns[i].Image = Resources.bu;
                    listBtns[i].BackColor = Color.LightGray;
                }
                else
                {
                    listBtns[i].Image = null;
                    listBtns[i].BackColor = Color.WhiteSmoke;
                }
            }
        }

        private void InitCtrls()
        {
            labelName.Text = chineseText;
            int count = listSels.Count;
            for (int i = 0; i < count; i++)
            {
                listBtns[i].Text = listSels[i];
            }
            switch (count)
            {
                case 1:
                    button1.Visible = true;
                    button2.Visible = false;
                    button3.Visible = false;
                    button1.Location = button2.Location;
                    this.Width = button1.Right + button1.Left;
                    break;
                case 2:
                    button1.Visible = true;
                    button2.Visible = true;
                    button3.Visible = false;
                   
                    this.Width = button2.Right + button1.Left;
                    break;
                case 3:
                    button1.Visible = true;
                    button2.Visible = true;
                    button3.Visible = true;                  
                    this.Width = button3.Right + button1.Left;
                    break;
                default:
                    break;
            }
            InitButtonOK();
            InitUserSel(0);
        }

        private void FormSelectPinYin_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            int i = listBtns.IndexOf(btn);
            if (i != -1)
            {
                InitUserSel(i);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listRadioBtns.Count; i++)
            {
                if (listRadioBtns[i].Checked)
                {
                    nSel = i;
                    break;
                }
            }    
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

    }
}
